//
//  ViewController.m
//  CIFilter
//
//  Created by 肖旭晖 on 2020/11/19.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) UIImageView *filterExample;
@property (nonatomic, strong) UIImageView *normalExample;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.normalExample = UIImageView.new;
    self.filterExample = UIImageView.new;
    
    self.normalExample.frame = CGRectMake(100, 100, 200, 200);
    self.filterExample.frame = CGRectMake(100, 350, 200, 200);
    
    self.normalExample.image = [UIImage imageNamed:@"example"];
    self.filterExample.image = self.filterImage;
    
    [self.view addSubview:self.normalExample];
    [self.view addSubview:self.filterExample];
}

-(UIImage *)filterImage{
    CIContext *context = [CIContext contextWithOptions:nil];
    UIImage *inputImage = [UIImage imageNamed:@"example"];
    CIImage *superImage = [CIImage imageWithCGImage:inputImage.CGImage];
    CIFilter *lighten = [CIFilter filterWithName:@"CIColorControls"];
    [lighten setValue:superImage forKey:kCIInputImageKey];

    // 修改亮度   -1---1   数越大越亮
    [lighten setValue:@(0.2) forKey:@"inputBrightness"];

    // 修改饱和度  0---2
    [lighten setValue:@(0.1) forKey:@"inputSaturation"];

    // 修改对比度  0---4
    [lighten setValue:@(0.4) forKey:@"inputContrast"];
    CIImage *result = [lighten valueForKey:kCIOutputImageKey];
    CGImageRef cgImage = [context createCGImage:result fromRect:[superImage extent]];
    // 得到修改后的图片
    UIImage *image = [UIImage imageWithCGImage:cgImage];

    // 释放对象
    CGImageRelease(cgImage);
    return image;
}

@end
